package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.data.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	private SortedListOf<ContactData> cachedContacts;
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	
	public SortedListOf<ContactData> getContacts() {
		if (this.cachedContacts == null) {
			rebuildCache();			
		} 
		return this.cachedContacts;
	
	}	

	private void rebuildCache() {
		this.cachedContacts = new SortedListOf<ContactData>();
	    manager.navigateTo().mainPage();
		List<WebElement> lines = driver.findElements(By.xpath("//tr"));			
		for (int i=1; i<lines.size()-1; i++) {						
			List<WebElement> cells = lines.get(i).findElements(By.tagName("td"));
			this.cachedContacts.add(new ContactData()
				.withLastName(cells.get(1).getText())
				.withFirstName(cells.get(2).getText()));
		}		
	}
	
	public ContactHelper createContact(ContactData contact, boolean typeForm) {
    	initContactCreation();
    	fillContactForm(contact, typeForm);
    	submitContactCreation();
    	returnToHomePage();
    	rebuildCache();
    	return this;
	}

	public ContactHelper modifyContact(int index, ContactData contact, boolean modifcation) {
		initContactModification(index);
		fillContactForm(contact, modifcation);
		submitContactModification();
		returnToHomePage();
		rebuildCache();
    	return this;
	}
	
	public void deleteContact(int index) {
		initContactModification(index);
		submitContactDeletion();
		returnToHomePage();
		rebuildCache();
	}
	
	
	//-------------------------------------------------------------------------------------------------------

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		this.cachedContacts = null;
		return this;
	}
	
	public ContactHelper initContactCreation() {
		manager.navigateTo().mainPage();
		click(By.linkText("add new"));
		return this;
	}
	
	public ContactHelper returnToHomePage() {
		click(By.linkText("home page"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstName());
		type(By.name("lastname"), contact.getLastName());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHome());
		type(By.name("mobile"), contact.getMobile());
		type(By.name("work"), contact.getWork());
		type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getEmail2());
	    selectByText(By.name("bday"), contact.getBirthDay());
	    selectByText(By.name("bmonth"), contact.getBirthdayMonth());
		type(By.name("byear"), contact.getBirthdayYear());
		if (formType = CREATION){
			selectByText(By.name("new_group"), contact.getGroup());
		}					
		type(By.name("address2"), contact.getAddress());
		type(By.name("phone2"), contact.getAddress2());
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("//*[@name='update' and @value='Update']"));
		this.cachedContacts = null;
		return this;
	}

	public ContactHelper initContactModification(int index) {
		manager.navigateTo().mainPage();
		click(By.xpath(".//*[@id='maintable']/tbody/tr["+ (2 + index) +"]/td[7]/a/img"));
		return this;
	}
	
	public ContactHelper submitContactDeletion() {		
		click(By.xpath("//*[@name='update' and @value='Delete']"));
		this.cachedContacts = null;
		return this;
	}

}
